package com.hz.tgb.api.invoice.ocr.hehe.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hz.tgb.response.restful.ResponseData;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 通用工具类
 *
 * @author hezhao on 2018/9/27 17:57
 */
public class CommonUtil {

    private CommonUtil() {
        // 私有类构造方法
    }

    public static String toString(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }

    public static Integer toInteger(Object obj) {
        return (obj == null) ? Integer.valueOf(0) : Integer.valueOf(toString(obj));
    }

    public static Long toLong(Object obj) {
        return (obj == null) ? Long.valueOf(0L) : Long.valueOf(toString(obj));
    }

    public static Double toDouble(Object obj) {
        return (obj == null) ? Double.valueOf(0.0D) : Double.valueOf(toString(obj));
    }

    public static Boolean toBoolean(Object obj) {
        return (obj == null) ? Boolean.valueOf(false) : Boolean.valueOf(toString(obj));
    }

    public static Map<String, Object> toMap(Object obj) {
        return (obj == null) ? new HashMap<>() : (Map)obj;
    }

    public static List<Map<String, Object>> toList(Object obj) {
        return (obj == null) ? new ArrayList<>() : (List<Map<String, Object>>)obj;
    }

    public static Double ifNull(Double value){
        return Objects.isNull(value) ? Double.valueOf(0.0D) : value;
    }

    public static BigDecimal ifNull(BigDecimal value){
        return Objects.isNull(value) ? BigDecimal.ZERO : value;
    }

    public static Integer ifNull(Integer value){
        return Objects.isNull(value) ? Integer.valueOf(0) : value;
    }

    public static <T> T ifNull(T value, T defaultValue){
        return Objects.isNull(value) ? defaultValue : value;
    }

    public static Double getNotNullDouble(Double num) {
        if (num == null){
            return 0.0D;
        }
        return new Double(num);
    }

    public static Double getNotNullDouble(BigDecimal num) {
        if (num == null){
            return 0.0D;
        }
        return num.doubleValue();
    }

    public static BigDecimal getNotNullBigDecimal(Double num) {
        if (num == null){
            return BigDecimal.ZERO;
        }
        BigDecimal decimal = new BigDecimal(String.valueOf(num));
        return decimal;
    }

    public static BigDecimal getNotNullBigDecimal(BigDecimal decimal) {
        if (decimal == null){
            return BigDecimal.ZERO;
        }
        return decimal;
    }

    public static Integer getNotNullInt(Integer num) {
        if (num == null){
            return 0;
        }
        return num;
    }

    public static Long getNotNullLong(Long num) {
        if (num == null){
            return 0L;
        }
        return num;
    }

    public static <T> List<T> getNotNullList(List<T> list) {
        if (list == null){
            return new ArrayList<>();
        }
        return list;
    }

    public static BigDecimal subtract(BigDecimal amount, BigDecimal... otherAmounts) {
        for (BigDecimal otherAmount : otherAmounts) {
            amount = getNotNullBigDecimal(amount).subtract(getNotNullBigDecimal(otherAmount));
        }
        return amount;
    }

    public static BigDecimal add(BigDecimal amount, BigDecimal... otherAmounts) {
        for (BigDecimal otherAmount : otherAmounts) {
            amount = getNotNullBigDecimal(amount).add(getNotNullBigDecimal(otherAmount));
        }
        return amount;
    }

    public static String toJSONStringNotNull(Object obj) {
        return toJSONStringNotNull(obj, "空");
    }

    public static String toJSONStringNotNull(Object obj, String defaultMsg) {
        return obj == null ? defaultMsg : JSON.toJSONString(obj);
    }

    public static String toJSONStringWriteMapNullValue(Object object) {
        return JSONObject.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
    }

    public static <T> String joinNotEmpty(T[] array, String splitStr, String defaultMsg) {
        return ArrayUtils.isEmpty(array) ? defaultMsg : StringUtils.join(array, splitStr);
    }

    public static <T> String joinNotEmpty(T[] array) {
        return ArrayUtils.isEmpty(array) ? "空" : StringUtils.join(array, ", ");
    }

    public static <T> String joinNotEmpty(List<T> list, String splitStr, String defaultMsg) {
        return CollectionUtils.isEmpty(list) ? defaultMsg : StringUtils.join(list, splitStr);
    }

    public static <T> String joinNotEmpty(List<T> list) {
        return CollectionUtils.isEmpty(list) ? "空" : StringUtils.join(list, ", ");
    }

    /**
     * 返回响应参数，过滤Null
     * @param responseData
     * @param <T>
     * @return
     */
    public static <T> T getResultData(ResponseData<T> responseData) {
        if (responseData != null && responseData.isSuccess()
                && responseData.getData() != null) {
            return responseData.getData();
        }
        return null;
    }

    public static <T> List<T> getResultListData(ResponseData<List<T>> responseData) {
        if (responseData != null && responseData.isSuccess()
                && CollectionUtils.isNotEmpty(responseData.getData())) {
            return responseData.getData();
        }
        return null;
    }

    public static <T> Page<T> getResultPageData(ResponseData<Page<T>> responseData) {
        if (responseData != null && responseData.isSuccess()
                && responseData.getData() != null && CollectionUtils.isEmpty(responseData.getData().getRecords())) {
            return responseData.getData();
        }
        return null;
    }

}
